clear
set matsize 5000
cd <directory>
set matsize 5000

cap log close
log using "tempgradient_spline_led0_umfe", replace

**********************************************************************************************************************
**Table 2- Columns 1-2 (Productivity-Temperature Gradient)
**********************************************************************************************************************

use "LEDprojecwithtamudatanew.dta" , clear

*drop post-LED observations
keep if led==0

*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

global depvars mactualeff

*residualize temperature and rainfall - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach i of varlist wbgtmeant1 wbgtmeant2 precipitation{
areg `i'  mbudgeteff _I*,  absorb(rno) cluster(unit)
predict `i'res, res

}

*residualize productivity -partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach i of varlist $depvars{
areg `i'   mbudgeteff _I*,  absorb(rno) cluster(unit)
predict `i'res, res
}

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end

foreach i of varlist $depvars{
foreach var of varlist wbgtmeant {

cap erase "eff_ledinteraction_yearbymonth_noled_`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_noled_`i'.txt"

cap erase "eff_ledinteraction_yearbymonth_noled_log`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_noled_log`i'.txt"
	
}
}

foreach i of varlist $depvars{

foreach var of varlist wbgtmeant {

	reg `i'res `var'1res `var'2res precipitationres,  cluster(unit)
			
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  `var'1res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	
	outreg2 using "eff_ledinteraction_yearbymonth_noled_`i'", stats(coef) noaster ctitle("rain control, LED") keep (`var'1res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  `var'2res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_noled_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (`var'2res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	
	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_noled_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

}
}

**********************************************************************************************************************
**Table 2 (Columns 3 and 4) - Interactions with the LED adoption dummy
**********************************************************************************************************************
use "LEDprojecwithtamudatanew.dta" , clear

*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

*Generate interactions of spline Variables for Temperature with LED adoption dummy
foreach var of varlist wbgtmeant {
foreach i of numlist 1/2{
g `var'`i'led=`var'`i'*led
replace `var'`i'led=. if `var'==. | led==.
}
}

global depvars mactualeff
*residualize temperature and rainfall,  LED dummmy interactions and LED dummy - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach i of varlist wbgtmeant1 wbgtmeant2 wbgtmeant1led wbgtmeant2led precipitation led{
areg `i'  mbudgeteff _I*,  absorb(rno) cluster(unit)
predict `i'res, res

}

*residualize productivity - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE
foreach i of varlist $depvars{
areg `i'  mbudgeteff  _I*,  absorb(rno) cluster(unit)
predict `i'res, res

}

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end
	
	
foreach i of varlist $depvars{
foreach var of varlist wbgtmeant {

cap erase "eff_ledinteraction_yearbymonth_`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_`i'.txt"

cap erase "eff_ledinteraction_yearbymonth_log`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_log`i'.txt"
	
}
}

foreach i of varlist $depvars{
foreach var of varlist wbgtmeant {

	reg `i'res `var'1ledres `var'2ledres `var'1res `var'2res ledres precipitationres,  cluster(unit)
			
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  `var'1res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	
	outreg2 using "eff_ledinteraction_yearbymonth_`i'", stats(coef) noaster ctitle("rain control, LED") keep (`var'1res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  `var'2res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (`var'2res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	*cap erase  eff_ledinteraction_yearbymonth_`i'.txt
	
	boottest  `var'1ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_`i'", stats(coef) noaster ctitle("rain control, LED") keep (`var'1ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest  `var'2ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (`var'2ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))


	
	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
}
}
